<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>My WebSocket</title>
    <style>
        #message {
            margin-top: 40px;
            border: 1px solid gray;
            padding: 20px;
        }
    </style>
</head>
<body>

<input type="text" id="nickname"/>

<button onclick="conectWebSocket()">连接WebSocket</button>
<button onclick="closeWebSocket()">断开连接</button>
<hr/>
<br/>
消息：<input id="text" type="text"/>
频道号：<input id="toUser" type="text"/>
<button onclick="send()">发送消息</button>
<div id="message"></div>
</body>
<!--<script src="./mui.min.js"></script>-->
<!--<script src="./app.js"></script>-->
<script type="text/javascript">
    var ChatMsg = function (senderId, receiverId, msg, msgId) {
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.msg = msg;
        this.msgId = msgId;
    };
    var DataContent = function (action, chatMsg, extand) {
        this.action = action;
        this.chatMsg = chatMsg;
        this.extand = extand;
    };
    /**
     * 和后端的枚举对应
     */
    var CONNECT = 1; 	// 第一次(或重连)初始化连接
    var CHAT = 2;		// 聊天消息
    var SIGNED = 3; 		// 消息签收
    var KEEPALIVE = 4; 	// 客户端保持心跳
    var PULL_FRIEND = 5;	// 重新拉取好友
    var websocket = null;

    function conectWebSocket() {
        var nickname = document.getElementById("nickname").value;
        if (nickname === "") {
            alert("请输入昵称");
            return;
        }
        if (websocket != null && websocket != undefined) {
            return false;
        }

        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            websocket = new WebSocket("ws://localhost:8088/chat");
        } else {
            alert('Not support websocket')
        }
        //连接发生错误的回调方法
        websocket.onerror = function () {
            setMessageInnerHTML("error");
        };
        //连接成功建立的回调方法
        websocket.onopen = function (event) {
            setMessageInnerHTML(" 成功建立连接");
            console.log("连接成功");
            //var me=app.getGlobeUserInfo();
            //构建chatMsg
            var chatMsg = {
                senderId : nickname,
                receiverId : null,
                msg : null,
                msgId : null
            }
            var dataContent = {
                action : 1,
                chatMsg : chatMsg,
                extand : null
            }
            // var chatMsg = ChatMsg(nickname, null, null, null);
            // var dataContent = DataContent(CONNECT, chatMsg, null);
            websocket.send(JSON.stringify(dataContent));
        }
        //接收到消息的回调方法
        websocket.onmessage = function (event) {
            setMessageInnerHTML(event.data);
        }
        //连接关闭的回调方法
        websocket.onclose = function () {
            setMessageInnerHTML("Loc MSG:关闭连接");
        }
        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            websocket.close();
        }
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //关闭连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send() {
        //获取输入的文本信息进行发送
        var nickname = document.getElementById("nickname").value;
        var message = document.getElementById('text').value;
        var toUser = document.getElementById('toUser').value;
        var socketMsg = {msg: message, toUser: toUser};
        if (toUser == '') {
            //群聊.
            socketMsg.type = 0;
        } else {
            //单聊.
            socketMsg.type = 1;
        }
        var chatMsg = {
            senderId : nickname,
            receiverId : toUser,
            msg : message,
            msgId : null
        }
        var dataContent = {
            action : 2,
            chatMsg : chatMsg,
            extand : null
        }
        // 构建ChatMsg
        //var chatMsg = ChatMsg(nickname, toUser, message, null);
        // 构建DataContent
       // var dataContent = DataContent(CHAT, chatMsg, null);
        websocket.send(JSON.stringify(dataContent));
    }
</script>
</html>